<?php
namespace submodule\modules\category\controllers;

use Yii;
//use backend\modules\mobo\models\BackendMenu;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;


use submodule\models\Models;
use submodule\models\ModelsField;
use common\components\Tree;
use base\YiiMobo;

use \common\models\DIYModel;

class IndexController extends \submodule\base\BaseSubmodelController
{


	
	public function init(){
		
		parent::init();

		
		$this->submodel->data_tablename = false;

	}

	/**
	 * 显示数据内容
     * 改变了默认的方法，读取数据、传递参数、分页更为自由
     * 模板文件默认为 views/city/index.php
	 */
	public function actionIndex(){
		$parentid = 0;
		$ajax = 1 ;
		$tr = '';
		$str = '';
		$indexFields = $this->getSubmodelIndexFields();
		if (!$indexFields){
			$this->OperTips(['backurl'=> $this->createUrl(['models/update', 'modelid'=>$this->submodel->modelid]), 'error'=> true, 'title'=> '请先进行后台首页设置', 'timeout'=> 3000]);
		}
		if (intval($this->submodel->models['models']['setting']['index']['ajax']) == 0){
			$list = Yii::$app->FileCache->get("models_data_".$this->submodel->modelid);	
			if(!$list){
				$list = $this->updateCategoryCache(true);
			}
			$function = $this->loadSubmoduleExtend('Backend', $this->submodel->models['models']['tablename'], $this->submodel->models, [], $this->submodel->modelid, $this->action->id);
			
			$tree = new Tree();
			$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			if ($list) foreach($list as $k=>$r) {
				$list[$k]['str_manage'] = '<a href="'.$this->getSubmodelActionUrl(['update','id'=>$r['id'], 'modelid'=>$this->submodel->modelid]).'" title="编辑" class="operation-update"> </a>&nbsp;&nbsp;<a href="'.$this->getSubmodelActionUrl(['delete','id'=>$r['id'], 'modelid'=>$this->submodel->modelid]).'" title="删除" class="operation-delete"> </a>';
				$url = $this->getSubmodelActionUrl(['status', 'locked'=>'-locked-', 'id'=>$r['id'], 'modelid'=>$this->submodel->modelid]);
				$list[$k]['display_button'] = '<div onclick="change_switch(this, \''.$url.'\');" id="btn-locked" class="locked-'.intval($r['status']).'"></div>';
				
				$list[$k]['click_img'] = '<img class="click_img" id="click_'.$r['id'].'" src="'.dirname(Yii::getAlias('@web')).'/static/common/images/left_e.gif'.'">&nbsp;&nbsp;';
				foreach($indexFields as $kk=>$val){
					if ($val['field'] == 'addtime' || $val['field'] == 'updatetime'){
						$list[$k][$val['field']] = date('Y-m-d', $r[$val['field']]);
					}else{
						$list[$k][$val['field']] = $this->loadSubmoduleValue($this->submodel->models, $val, $r, $function, 'Index');
					}
				}
			}

			$ajax = 0;
			$str .= "<td align='center' width='20'><input type='checkbox' value='\$id' name='checked_id[]'/></td>";
			foreach($indexFields as $k=>$val){
				if ($val['field'] == 'listorder'){
					$str .= "<td align='center'><input name='listorder[\$id]' type='text' size='3' value='\$listorder' class='input-text input-listorder'></td>";
				}else if ($val['field'] == 'name'){
					$str .= "<td >\$spacer<input name='name[\$id]' type='text' style='width:150px' value='\$name' class='input-text input-name'></td>"; 
				}else	if ($val['field'] == 'status'){
					$str .= "<td class='td-locked'>\$display_button</td>";
				}else{
					$str .= "<td >\${$val['field']}</td>"; 
				}
			}
			$str .= "<td align='center'  class='td-operation'>\$str_manage</td>";
			$str .= "</tr>";
			$tree->init($list);
            $tr = $tree->get_tree(0,$str, $parentid);
		}
		
		
		

		$views = @$this->submodel->models['models']['setting']['index']['views'];
		$viewsfile = $views ? $views : ($ajax ? "@submodule/modules/category/views/index/category_index-ajax" : "@submodule/modules/category/views/index/category_index");
		echo $this->render($viewsfile, [
		    'models' => $this->submodel->models,
			'indexFields' => $indexFields,
            'tr'=>$tr,
		]);
		
	}

}
